package com.chainyoung.component_tmperson.ui.mvp.model;


import com.chainyoung.common.appcommon.db.DbConstant;
import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.appcommon.hdwallet.EtherscanService;
import com.chainyoung.common.appcommon.hdwallet.HdWalletDAO;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.di.scope.FragmentScope;
import com.chainyoung.common.integration.IRepositoryManager;
import com.chainyoung.common.jsoup.JP;
import com.chainyoung.common.mvp.BaseModel;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletLocalContract;
import com.google.gson.Gson;
import com.google.gson.JsonArray;

import org.json.JSONObject;
import org.jsoup.Jsoup;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;

@FragmentScope
public class WalletLocalModel extends BaseModel implements WalletLocalContract.Model {

    private HdWalletDAO DAO = new HdWalletDAO(mRepositoryManager.obtainRealmConfig(DbConstant.DB_NAME));


    @Inject
    WalletLocalModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);

    }


    @Override
    public Flowable<String> getSingleEthBalance(String addr) {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getBalance(addr)
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        String balance = new JSONObject(body.string()).getString("result");
                        if (balance.equals("0")) {
                            return "0";
                        } else {
                            return balance;
                        }
                    }
                });
    }


    @Override
    public Flowable<String> getUsdCnyRate(String from, String to) {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getUsdCnyRate(from, to)
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        return new JSONObject(body.string()).getString("data");
                    }
                });
    }


    @Override
    public Flowable<String> getEthPrice() {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getEthPrice()
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        return new JSONObject(body.string()).getString("result");
                    }
                });
    }

    @Override
    public Flowable<List<HdWalletCache>> getAllHdWalletCaches(String coinType) {
        return DAO.getHdWalletCaches(coinType);
    }


    @Override
    public Flowable<HdWalletCache> addHdWalletCache(HdWalletCache item) {
        return DAO.addHdWalletCache(item);
    }

}